from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from controller.SysDictAdminController import SysDictAdminController
from schema.ParamSchema import SysDictInfo, SysDictDetailInfo
from schema.QuerySchema import QueryBase
from util.Auth import check_token
from util.DataBaseTools import get_db_session
from util.ErrorCode import ErrorCode
from util.LogHelper import Log

dict_admin_router = APIRouter()

@dict_admin_router.post("/param/dict/add", summary='添加字典信息')
def add_sys_dict(sysDictInfo: SysDictInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/param/dict/add')
    if result.get('code') == 0:
        if result.get('data').port_id == 0:
            response = SysDictAdminController.add_sys_dict(sysDictInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@dict_admin_router.post("/param/dict/mod", summary='修改字典信息')
def mod_sys_dict(sysDictInfo: SysDictInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/param/dict/mod')
    if result.get('code') == 0:
        if result.get('data').port_id == 0:
            response = SysDictAdminController.mod_sys_dict(sysDictInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@dict_admin_router.get("/param/dict/query/{sys_dict_id}", summary='根据字典id查询字典信息')
def query_sys_dict_by_id(sys_dict_id: int, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/param/dict/query/sys_dict_id={sys_dict_id}')
    if result.get('code') == 0:
        response = SysDictAdminController.query_sys_dict_by_id(sys_dict_id, db)
        return response
    else:
        return result


@dict_admin_router.post("/param/dict/querybypage", summary='分页查询字典信息表所有信息')
def query_sys_dict_all(query_base: QueryBase, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/param/dict/querybypage')
    if result.get('code') == 0:
        response = SysDictAdminController.query_sys_dict_by_page(query_base, db)
        return response
    else:
        return result


@dict_admin_router.get("/param/dict/queryall/detail", summary='查询字典信息(包含字典明细)所有信息')
def query_sys_dict_all_detail(db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/param/dict/queryall/detail')
    if result.get('code') == 0:
        response = SysDictAdminController.query_sys_dict_all_detail(db)
        return response
    else:
        return result


@dict_admin_router.delete("/param/dict/delete/{sys_dict_id}", summary='根据字典id删除字典信息')
def delete_sys_dict(sys_dict_id: int, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/param/dict/delete/sys_dict_id={sys_dict_id}')
    if result.get('code') == 0:
        if result.get('data').port_id == 0:
            response = SysDictAdminController.delete_sys_dict(sys_dict_id, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@dict_admin_router.post("/param/dictdetail/add", summary='添加字典明细信息')
def add_sys_dict_detail(sysDictDetailInfo: SysDictDetailInfo, db: Session = Depends(get_db_session),
                        result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/param/dictdetail/add')
    if result.get('code') == 0:
        if result.get('data').port_id == 0:
            response = SysDictAdminController.add_sys_dict_detail(sysDictDetailInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@dict_admin_router.post("/param/dictdetail/mod", summary='修改字典明细信息')
def mod_sys_dict_detail(sysDictDetailInfo: SysDictDetailInfo, db: Session = Depends(get_db_session),
                        result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/param/dictdetail/mod')
    if result.get('code') == 0:
        if result.get('data').port_id == 0:
            response = SysDictAdminController.mod_sys_dict_detail(sysDictDetailInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@dict_admin_router.get("/param/dictdetail/query/{sys_dict_id}", summary='根据字典id查询字典明细信息表')
def query_sys_dict_detail_by_id(sys_dict_id: int, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/param/dictdetail/query/sys_dict_id={sys_dict_id}')
    if result.get('code') == 0:
        response = SysDictAdminController.query_sys_dict_detail_by_id(sys_dict_id, db)
        return response
    else:
        return result


@dict_admin_router.delete("/param/dictdetail/delete", summary='根据字典id和字典明细id删除字典明细信息')
def delete_sys_dict(sysDictDetailInfo: SysDictDetailInfo, db: Session = Depends(get_db_session),
                    result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/param/dictdetail/delete')
    if result.get('code') == 0:
        if result.get('data').port_id == 0:
            response = SysDictAdminController.delete_sys_dict_detail(sysDictDetailInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result